<?
?>

<style>
    div.br{
        padding: 20px;
        border : 1px solid #ddd;
    }
    div.br div.item{
        overflow: hidden;
        margin-bottom: 10px;
    }
    div.br div.item a{
        float: left;

    }
    div.br div.item img{
        float: left;
        padding: 5px;
        border : 1px solid #ddd;
    }

</style>
<?php
if (!is_array($items)) {
    $items = array();
}
?>
<div class=br ms-controller="banner">
    <a href="javascript:;" class="btn" ms-click="add">添加一张图片</a>
    <div style="height: 10px;"></div>
    <div class="list">
        <div class="item" ms-repeat="items">
            <img ms-attr-link="{{el.link}}" ms-attr-src="{{el.pic}}" alt="" style="width: 800px;height: 400px;" >
            <a href="javascript:;" class="btn" ms-click="$remove" style="margin-left: 10px;">删除</a>
        </div>
    </div>
</div>
<script>

    var banner = avalon.define({
        $id : 'banner',
        items : [],
        add : function () {
            banner.items.push({
                pic : '/data/upload/shop/common/default_goods_image.gif',
                link : ""
            })
        }
    });

    banner.items = <?php echo json_encode($items);?>;
    

</script>

<script>
    $.getJSON("/shop/templates/<?php echo $config['template_name']?>/images.json", function (json) {
        window.save_json = function () {
            $('tr.name').each(function () {
                var _this = $(this);
                var name = _this.find('td').text().replace("：", "").trim()
                if (json[name]) {
                    if (name == '首页轮播') {
                        var arr = [];
                        $('.br .item img').each(function () {
                            arr.push({
                                pic: this.getAttribute('src'),
                                link: this.getAttribute("link")
                            })
                        });
                        json[name].items = arr;
                    }
                    else if(name == '基本设置'){
                        json[name].items = [
                            {
                                color : $('[name=color]').val(),
                                qq : $('[name=qq]').val(),
                                search : $('[name=_search]').val(),
                                language : $('[name=_language]').val()
                            },
                            {
                                nav : {
                                    pic :  $('[name=nav]').attr('src'),
                                    link :  $('[name=nav]').attr('link')
                                }
                            }
                        ]
                    }
                    else if(name == '中部广告位'){
                        json[name].items = [
                            {
                                pic : $('.mid_pic img').eq(0).attr('src'),
                                link : $('.mid_pic img').eq(0).attr('link'),
                            },
                            {
                                pic : $('.mid_pic img').eq(1).attr('src'),
                                link : $('.mid_pic img').eq(1).attr('link'),
                            },
                            {
                                pic : $('.mid_pic img').eq(2).attr('src'),
                                link : $('.mid_pic img').eq(2).attr('link'),
                            }
                        ]
                    }

                    else if (name == '楼层') {
                        var arr = floors.items.$model;
                        $('.floor section').each(function (index) {
                            if($(this).hasClass('floor_type2')){
                                $(this).find('img').each(function (i) {
                                    var k = i + 1;
                                    arr[index]['img' + k]['pic'] = $(this).attr('src');
                                    arr[index]['img' + k]['link'] = $(this).attr('link');
                                })
                            }
                            if($(this).hasClass('floor_type3')){
                                arr[index]['img'] = {
                                    pic: $(this).find('img').attr('src'),
                                    link: $(this).find('img').attr('link')

                                };
                            }

                        })
                        json[name].items = arr
                    }

                    else {
                        json[name].pic = _this.next().find('img[link]').attr('src');
                        json[name].link = _this.next().find("img[link]").attr('link')
                    }
                }

            });
            console.log(json);
            var index = layer.load();
            $.post("/admin/?act=web_config&op=save_json", json, function () {
                layer.close(index);
                layer.msg("保存成功", function () {
                    location.reload();
                })
            });

        };
    });
</script>
